import { BaseEntity } from '@libs/common/entities';
import { ApiProperty } from '@nestjs/swagger';
import { Dayjs } from '@shencom/utils-date';
import { Transform, Type } from 'class-transformer';
import { Column, Entity, Index } from 'typeorm';

export const ColumnEnum = {
  //  153439396:'attendanceClass',
  // 153439417: 'attendResult',
  // 153439404: 'absenteeismLateTimes',
  offDutyUserCheckResult: { id: 153439422, name: '下班1打卡结果' },
  offDutyUserCheckTime: { id: 153439421, name: '下班1打卡时间' },
  attendanceDays: { id: 153439397, name: '出勤天数' },
  shouldAttendanceDays: { id: 153439394, name: '应出勤天数' },
  leaveEarlyMinute: { id: 153439406, name: '早退时长' },
  absenteeismDays: { id: 153439409, name: '未打卡天数' }, // 旷工天数
  onDutyUserCheckResult: { id: 153439420, name: '上班1打卡结果' },
  onDutyUserCheckTime: { id: 153439419, name: '上班1打卡时间' },
  planDetail: { id: 153439418, name: '班次' },
  attendanceRestDays: { id: 153439398, name: '休息天数' },
  attendanceWorkTime: { id: 153439399, name: '工作时长' },
  lateMinute: { id: 153439401, name: '迟到时长' },
  lateTimes: { id: 153439400, name: '迟到次数' },
  leaveEarlyTimes: { id: 153439405, name: '早退次数' },
  makingUpLackTimes: { id: 153439395, name: '补卡次数' },
  seriousLateTimes: { id: 153439402, name: '严重迟到次数' },
  seriousLateMinute: { id: 153439403, name: '严重迟到时长' },
  onWorkLackCardTimes: { id: 153439407, name: '上班缺卡次数' },
  offWorkLackCardTimes: { id: 153439408, name: '下班缺卡次数' },
} as const;

@Entity('crm_node_hr_attendance_statistic')
@Index(['dingId', 'workDate'], { unique: true })
export class AttendanceStatistic extends BaseEntity {
  @ApiProperty({ description: '钉钉 id' })
  @Column({ comment: '钉钉 id' })
  dingId: string;

  @ApiProperty({ description: '用户名' })
  @Column({ comment: '用户名' })
  userName: string;

  @ApiProperty({ description: '用户ID' })
  @Column({ comment: '用户ID', nullable: true })
  userId?: string;

  @ApiProperty({ description: '部门' })
  @Column({ comment: '部门', nullable: true })
  department?: string;

  @ApiProperty({ description: '考勤组' })
  @Column({ comment: '考勤组', nullable: true })
  group?: string;

  @ApiProperty({ description: ColumnEnum.offDutyUserCheckResult.name })
  @Column({ comment: ColumnEnum.offDutyUserCheckResult.name, nullable: true }) // 153439422
  offDutyUserCheckResult: string;

  @ApiProperty({ description: ColumnEnum.offDutyUserCheckTime.name })
  @Column({ comment: ColumnEnum.offDutyUserCheckTime.name, nullable: true }) // 153439421
  offDutyUserCheckTime: string;

  @ApiProperty({ description: ColumnEnum.onDutyUserCheckResult.name })
  @Column({ comment: ColumnEnum.onDutyUserCheckResult.name, nullable: true }) // 153439420
  onDutyUserCheckResult: string;

  @ApiProperty({ description: ColumnEnum.onDutyUserCheckTime.name })
  @Column({ comment: ColumnEnum.onDutyUserCheckTime.name, nullable: true }) // 153439419
  onDutyUserCheckTime: string;

  @ApiProperty({ description: ColumnEnum.absenteeismDays.name })
  @Column({ comment: ColumnEnum.absenteeismDays.name, nullable: true }) // 153439409
  @Type(() => Number)
  absenteeismDays: number;

  // @Column({ comment: '旷工迟到次数' }) // 153439404
  // absenteeismLateTimes: number;

  // @Column({ comment: '出勤班次' }) // 153439396
  // attendanceClass: number;

  @ApiProperty({ description: ColumnEnum.attendanceDays.name })
  @Column({
    type: 'double',
    comment: ColumnEnum.attendanceDays.name,
    nullable: true,
  }) // 153439397
  @Type(() => Number)
  attendanceDays: number;

  @ApiProperty({ description: ColumnEnum.attendanceRestDays.name })
  @Column({ comment: ColumnEnum.attendanceRestDays.name, nullable: true }) // 153439398
  @Type(() => Number)
  attendanceRestDays: number;

  @ApiProperty({ description: ColumnEnum.attendanceWorkTime.name })
  @Column({ comment: ColumnEnum.attendanceWorkTime.name, nullable: true }) // 153439399
  @Type(() => Number)
  attendanceWorkTime: number;

  // @Column({ comment: '考勤结果', nullable: true }) // 153439417
  // attendResult: string;

  @ApiProperty({ description: ColumnEnum.lateMinute.name })
  @Column({ comment: ColumnEnum.lateMinute.name, nullable: true }) // 153439401
  @Type(() => Number)
  lateMinute: number;

  @ApiProperty({ description: ColumnEnum.lateTimes.name })
  @Column({ comment: ColumnEnum.lateTimes.name, nullable: true }) // 153439400
  @Type(() => Number)
  lateTimes: number;

  @ApiProperty({ description: ColumnEnum.leaveEarlyMinute.name })
  @Column({ comment: ColumnEnum.leaveEarlyMinute.name, nullable: true }) // 153439406
  @Type(() => Number)
  leaveEarlyMinute: number;

  @ApiProperty({ description: ColumnEnum.leaveEarlyTimes.name })
  @Column({ comment: ColumnEnum.leaveEarlyTimes.name, nullable: true }) // 153439405
  @Type(() => Number)
  leaveEarlyTimes: number;

  @ApiProperty({ description: ColumnEnum.makingUpLackTimes.name })
  @Column({ comment: ColumnEnum.makingUpLackTimes.name, nullable: true }) // 153439395
  @Type(() => Number)
  makingUpLackTimes: number;

  @ApiProperty({ description: ColumnEnum.offWorkLackCardTimes.name })
  @Column({ comment: ColumnEnum.offWorkLackCardTimes.name, nullable: true }) // 153439408
  @Type(() => Number)
  offWorkLackCardTimes: number;

  @ApiProperty({ description: ColumnEnum.onWorkLackCardTimes.name })
  @Column({ comment: ColumnEnum.onWorkLackCardTimes.name, nullable: true }) // 153439407
  @Type(() => Number)
  onWorkLackCardTimes: number;

  @ApiProperty({ description: ColumnEnum.planDetail.name })
  @Column({ comment: ColumnEnum.planDetail.name, nullable: true }) // 153439418
  planDetail: string;

  @ApiProperty({ description: ColumnEnum.seriousLateMinute.name })
  @Column({ comment: ColumnEnum.seriousLateMinute.name, nullable: true }) // 153439403
  @Type(() => Number)
  seriousLateMinute: number;

  @ApiProperty({ description: ColumnEnum.seriousLateTimes.name })
  @Column({ comment: ColumnEnum.seriousLateTimes.name, nullable: true }) // 153439402
  @Type(() => Number)
  seriousLateTimes: number;

  @ApiProperty({ description: ColumnEnum.shouldAttendanceDays.name })
  @Column({ comment: ColumnEnum.shouldAttendanceDays.name, nullable: true }) // 153439394
  @Type(() => Number)
  shouldAttendanceDays: number;

  @ApiProperty({ description: '考勤时间' })
  @Column({ type: 'datetime', comment: '考勤时间' })
  @Transform((params) => {
    const { value, type } = params;
    if (type === 1) {
      return Dayjs(value).valueOf();
    }

    if (typeof value === 'string') {
      if (!isNaN(+value)) return Dayjs(+value).toDate();
      else return Dayjs(value).toDate();
    }
  })
  workDate: Date | string;
}
